﻿using System;
using System.Xml.Linq;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using System.IO;
namespace Skyper.Model {
	public sealed class SkypeConfig : BaseModel {
		#region Fields
		private String hostname = @"http://localhost:6789/";
		private String password;
		private Boolean autoLogin = false;
		private Boolean autoSync = true;

		private DateTime lastContactSync;
		private DateTime lastAvatarSync;
		#endregion
		#region Public
		/// <summary>
		/// Suspends this model to XElement.
		/// </summary>
		/// <returns>
		/// XElement or Null if suspending is not possible.
		/// </returns>
		public sealed override XElement Suspend() {
			XElement node = new XElement("Config");

			node.SetAttributeValue("Hostname", hostname);
			node.SetAttributeValue("Password", password);
			node.SetAttributeValue("AutoLogin", autoLogin);
			node.SetAttributeValue("AutoSync", autoSync);

			return node;
		}
		/// <summary>
		/// Resumes this element, with specified node.
		/// </summary>
		/// <param name="node">The node.</param>
		/// <returns>
		/// True, if resuming proccess was success.
		/// </returns>
		public sealed override bool Resume(XElement node) {
			if (node == null || node.Name != "Config") {
				return false;
			}

			if (node.HasAttributes) {
				hostname = node.GetAttributeValue("Hostname");
				password = node.GetAttributeValue("Password");

				node.TryGetAttributeValue("AutoLogin", out autoLogin);
				node.TryGetAttributeValue("AutoSync", out autoSync);
			}

			return true;
		}

		/// <summary>
		/// Saves users config.
		/// </summary>
		public void Save() {
			try {
				using (var sf = IsolatedStorageFile.GetUserStoreForApplication()) {
					using (var fs = sf.OpenFile("/Config.xml", FileMode.Create, FileAccess.Write, FileShare.None)) {
						XElement node = this.Suspend();

						XDocument doc = new XDocument();
						doc.Add(node);
						doc.Save(fs);
					}
				}
			}
#if DEBUG
 catch (Exception exc) {
	 if (Debugger.IsAttached) {
		 Debugger.Break();
	 }
	 Debug.WriteLine(exc);
			}
#else
			catch {}
#endif
		}
		/// <summary>
		/// Loads users config.
		/// </summary>
		public void Load() {
			try {
				using (var sf = IsolatedStorageFile.GetUserStoreForApplication()) {
					if (sf.FileExists("/Config.xml")) {
						//ALrighty then!

						using (var fs = sf.OpenFile("/Config.xml", FileMode.Open, FileAccess.Read, FileShare.None)) {
							XDocument doc = XDocument.Load(fs);

							this.Resume(doc.FirstNode as XElement);
						}
					}
				}
			}
#if DEBUG
 catch (Exception exc) {
	 if (Debugger.IsAttached) {
		 Debugger.Break();
	 }
	 Debug.WriteLine(exc);
			}
#else
			catch {}
#endif
		}
		#endregion
		#region Property
		/// <summary>
		/// Gets or sets the hostname.
		/// </summary>
		/// <value>
		/// The hostname.
		/// </value>
		public String Hostname {
			get { return this.hostname; }
			set {
				if (this.hostname != value) {
					this.hostname = value;
					this.OnPropertyChanged("Hostname");
				}
			}
		}
		/// <summary>
		/// Gets or sets the password.
		/// </summary>
		/// <value>
		/// The password.
		/// </value>
		public String Password {
			get { return this.password; }
			set {
				if (this.password != value) {
					this.password = value;
					this.OnPropertyChanged("Password");
				}
			}
		}
		/// <summary>
		/// Gets or sets a value indicating whether skyper should auto login.
		/// </summary>
		/// <value>
		///   <c>true</c> if auto login; otherwise, <c>false</c>.
		/// </value>
		public Boolean AutoLogin {
			get { return this.autoLogin; }
			set {
				if (this.autoLogin != value) {
					this.autoLogin = value;
					this.OnPropertyChanged("AutoLogin");
				}
			}
		}
		/// <summary>
		/// Gets or sets a value indicating whether skyper should auto sync.
		/// </summary>
		/// <value>
		///   <c>true</c> if skyper should auto sync; otherwise, <c>false</c>.
		/// </value>
		public Boolean AutoSync {
			get { return this.autoSync; }
			set {
				if (this.autoSync != value) {
					this.autoSync = value;
					this.OnPropertyChanged("AutoSync");
				}
			}
		}

		public DateTime LastContactSync {
			get { return this.lastContactSync; }
			set {
				if (this.lastContactSync != value) {
					this.lastContactSync = value;
					this.OnPropertyChanged("LastContactSync");
				}
			}
		}
		public DateTime LastAvatarSync {
			get { return this.lastAvatarSync; }
			set {
				if (this.lastAvatarSync != value) {
					this.lastAvatarSync = value;
					this.OnPropertyChanged("LastAvatarSync");
				}
			}
		}
		#endregion
	}
}
